#微信跳一跳外挂
import time,random
import cv2
import numpy as np
import os,sys,subprocess

def get_distance(i):
    jump = cv2.imread("images/runtime/jump_%d.png"%i, cv2.IMREAD_GRAYSCALE)
    jump = cv2.resize(jump, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
    circle_temp = cv2.imread("templates/circle_temp.png",cv2.IMREAD_GRAYSCALE)
    qizi_temp = cv2.imread("templates/qi_zi_temp.png", cv2.IMREAD_GRAYSCALE)
    w, h = qizi_temp.shape[::-1]

    # 计算棋子位置
    #res = cv2.matchTemplate(jump, qizi_temp, cv2.TM_SQDIFF_NORMED)
    res = cv2.matchTemplate(jump, qizi_temp, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    x1 = int(top_left[0] + 20)
    y1 = int(top_left[1] + 67)
    # print(x1, y1)

    # 计算目标位置
    res = cv2.matchTemplate(jump, circle_temp, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if max_val>1:
        print("circle")
        x2 = max_loc[0]+9
        y2 = max_loc[1]+6
    else:
        # 1.高斯滤波(有利于边缘提取)
        jump = cv2.GaussianBlur(jump, (5, 5), 0)
        # 2.边缘提取
        canny = cv2.Canny(jump, 1, 10)

        # 3.去掉小棋子的影响
        for row in range(top_left[1], top_left[1] + h):
            for col in range(top_left[0], top_left[0] + w):
                canny[row][col] = 0

        # 3.从200行开始向下扫描找到值为1的行
        y2_top = np.nonzero([row for row in canny[200:]])[0][0] + 200

        # 4.在上述行上向右扫描找到值为1的列
        x2_top = int(np.mean(np.nonzero(canny[y2_top])))
        # print(x2_top, y2_top)
        x2 = x2_top

        # 5.在上述列向下扫描找到值为1的行
        y2_bottom = y2_top + 100 - np.nonzero(canny[y2_top + 100::-1, x2_top])[0][0]
        #y2_bottom = np.nonzero(canny[y2_top+1::, x2_top])[0][0]+y2_top+1
        y2 = (y2_bottom + y2_top) / 2

    #每10次记录一次
    if i<20:
        cv2.rectangle(jump, top_left, bottom_right, 1, 2)
        cv2.line(jump, (int(x1), int(y1)), (int(x2), int(y2)), 0, 4)
        cv2.imwrite("images/runtime/jump_%d_canny.png"%i, jump)

    #print((x1, y1), (x2, y2))
    # 返回2点之间的距离
    return ((x2-x1)**2+(y2-y1)**2)**0.5

def jump_by_distance(distance):
    jump_time = int(distance * 4.25)
    print(distance, jump_time)

    # 生成随机触摸点
    rand = int(random.random() * 200)
    # sb.stdin.write(("input swipe %i %i %i %i "+str(jump_time)) % (320+rand,410+rand,320+rand,410+rand))
    os.system((os.path.join(basepath, "adb.exe") +
               " -s %s shell input swipe %i %i %i %i " + str(jump_time))
              % (ip, 100 + rand, 400 + rand, 100 + rand, 400 + rand))

basepath=os.getcwd()
#1.使用adb工具截屏
params=sys.argv
if len(params)<2:
    print("请输入adb服务器的ip+port")
    exit(0)
ip=params[1]
#连接adb服务器
os.system(os.path.join(basepath,"adb.exe")+" connect %s" % ip)

#进入adb shell
#sb=subprocess.Popen(os.path.join(basepath, "adb.exe")+" shell",
#                stdout=subprocess.PIPE ,stdin=subprocess.PIPE,stderr=subprocess.PIPE,
#               encoding="utf8")

#第一次跳
jump_by_distance(173)
time.sleep(1)

#2.主逻辑
for i in range(1,1000):
    #手机截图
    os.system(os.path.join(basepath, "adb.exe") + " -s %s shell screencap /sdcard/jump.png"%ip)
    os.system(os.path.join(basepath, "adb.exe") + " -s %s pull /sdcard/jump.png "%ip + os.path.join(basepath,"images","runtime","jump_%d.png"%i))

    #计算触摸时间
    distance=get_distance(i)
    distance=int(distance)
    jump_by_distance(distance)

    #随机休眠0-1秒
    time.sleep(random.random()+2)